package org.xteam.engine.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.xteam.engine.geom.Point3D;
import org.xteam.engine.object.Triangle;

public class OBJReader {

	public List<Triangle> read(Reader reader) throws IOException {
		List<Triangle> triangles = new ArrayList<Triangle>();
		List<Point3D> points = new ArrayList<Point3D>();
		BufferedReader lineReader = new BufferedReader(reader);
		String line;
		while ((line = lineReader.readLine()) != null) {
			String[] elements = line.split(" ");
			if (elements[0].equals("v")) {
				points.add(new Point3D(
						Double.parseDouble(elements[1]),
						Double.parseDouble(elements[2]),
						Double.parseDouble(elements[3])));
			} else if (elements[0].equals("f")) {
				triangles.add(new Triangle(
						points.get(Integer.parseInt(elements[1])-1),
						points.get(Integer.parseInt(elements[2])-1),
						points.get(Integer.parseInt(elements[3])-1)));
			}
		}
		return triangles;
	}
}
